package com.hr.dao;

import com.hr.model.Archive;
import com.hr.model.Department;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface DepartmentDAO
{

    @Results(id="department",value = {
            @Result(id = true, property = "id", column = "dep_id"),
            @Result(property = "name", column = "dep_name"),
            @Result(property = "intro", column = "dep_intro"),
            @Result(property = "mail", column = "dep_mail"),
            @Result(property = "intro", column = "dep_intro"),

//            @Result(property = "archives", column = "dep_id", many = @Many(select = "com.hr.dao.ArchiveDAO.getArchivesByDepId")),
              })
    @Select("select * from department where dep_id=#{depid}")
    Department getDepartmentByDepId(@Param("depid")int depid);

    @ResultMap("department")
    @Select("select * from department where c_id=#{cid}")
    List<Department> getDepartmentsByCId(@Param("cid")int cid);

    @SelectKey(keyProperty = "department.id",before = false,resultType = Integer.class,statement = "select last_insert_id()")
    @Insert("insert into department (c_id, dep_name, dep_intro) values (#{cid}, #{department.name}, #{department.intro})")
    void insertDepartment(@Param("cid")int cid,@Param("department")Department department);

    @Update("update  department set dep_name=#{department.name},dep_intro=#{department.intro} where dep_id=#{department.id}")
    void updateDepartment(@Param("department")Department department);

    @Delete("delete from department where dep_id=#{depid}")
    void deleteDepartmnet(@Param("depid")int depid);

}
